* Code to reproduce “Table IV. A Spatial Durbin Model for the intensity effect of local spending on OCV in 2010”, using the integrated “Dez_and_Death_integrated_dataset.dta” – 50 km radius specification

* Merge with georeferenced dataset
keep if year ==2010
merge code using Cabezera_Coordinados

* For the spmlreg to work, the dataset has to be balanced and squared
drop if CIDE_OC_deaths_Cap_Log ==.
drop if lat_dd==.

* The following set of commands does not work if islands (i.e., those local governments who do not have a neighbour with a positive OCG homicide rate in the below applied radius) are included, which is why islands are dropped here
drop if code == 8052
drop if code == 23004
drop if code == 31050
drop if code ==3002
drop if code == 3003
drop if code ==3008
drop if code ==5010
drop if code == 28027
drop if code == 2001
drop if code == 2002
drop if code == 4003
drop if code == 5002
drop if code == 5025
drop if code ==  5032
drop if code == 7008
drop if code == 7017
drop if code == 7034
drop if code == 7065
drop if code == 7101
drop if code == 7107
drop if code == 8001
drop if code == 8009
drop if code == 8010
drop if code == 8027
drop if code == 8029
drop if code == 8037
drop if code == 8048
drop if code == 10019
drop if code == 10023
drop if code == 10024
drop if code == 10026
drop if code == 18009
drop if code == 19044
drop if code == 23005
drop if code == 22009
drop if code == 23009
drop if code == 24024
drop if code == 25001
drop if code == 25005
drop if code == 26003
drop if code == 26014
drop if code == 26019
drop if code == 26024
drop if code == 26044
drop if code == 26048
drop if code == 26051
drop if code == 26055
drop if code == 26070
drop if code == 27003
drop if code == 28012
drop if code == 32022
drop if code == 32039

* Generating the binary spatial weights matrix including municipalities within a 50 km range
capture spwmatrix gecon lat_dd long_dd , wn(Binary_50km) wtype(bin) db(0 50) row mataf eignvar(eigenvalues) noisland

* Computation of the  part of the “Spatial Durbin Model for the intensity effect of local spending on OCV in 2010”, whereas the variables related to security spending per capita, the local police pear capita, spending per capita, revenues per capita and population density are multiply imputed with 50 datased (M=50) 
tabulate Estado_ID, gen(Estado)	
mi set mlong
mi register imputed Sec_Spending_Cap_2010 Loc_Police_Per_Head Spending_Cap_Real Own_Revenue_Share Population_Density  
mi register regular CIDE_OC_deaths_Cap_Log gini poverty Share_Head_of_HH_Female Household_Size Population_15_29_Males  Males_Per_100_Females Indigenous_Pop_Share Population Munic_Size_Categ Laakso_Tag_Ind Dummy_Border_PCoast Estado1-Estado29
mi impute mvn Sec_Spending_Cap_2010 Loc_Police_Per_Head Spending_Cap_Real Own_Revenue_Share Population_Density    = CIDE_OC_deaths_Cap_Log gini poverty Share_Head_of_HH_Female Household_Size Population_15_29_Males  Males_Per_100_Females Indigenous_Pop_Share Population Munic_Size_Categ Laakso_Tag_Ind Dummy_Border_PCoast Estado1-Estado29, add(50) force
mi xeq 0 1 20 50: summarize Sec_Spending_Cap_2010 Loc_Police_Per_Head Spending_Cap_Real Own_Revenue_Share Population_Density  
mi estimate, cmdok: spmlreg CIDE_OC_deaths_Cap_Log Spending_Cap_Real Own_Revenue_Share Sec_Spending_Cap_2010 Loc_Police_Per_Head gini poverty Share_Head_of_HH_Female Household_Size Population_Density  Population_15_29_Males  Males_Per_100_Females Indigenous_Pop_Share Population Munic_Size_Categ Laakso_Tag_Ind Dummy_Border_PCoast Estado1-Estado29, weights(Binary_50km) wfrom(Mata) eignvar(eigenvalues) model(durbin) robust

*Results and important additional parameters are stored in an excel file
putexcel set "resultsCS_SDM.xlsx", sheet("OLS_Bin_50km") modify
putexcel I3=(" Number of obs")
putexcel J3=( e(N_mi))
putexcel I5=("F_mi")
putexcel J5=( e(F_mi))
matrix a = r(table)'
matrix a = a[., 1..6]
putexcel A2=matrix(a)
